package com.activity;

import maps.test.R;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.PrayTime.PrayTimeLayout;
import com.commun.AjouterMosque;
import com.commun.Param;
import com.commun.Utils;
import com.data.DataBaseQuery;
import com.geo.GeoTools;
import com.geo.MyMapView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MyLocationOverlay;
import com.service.AlarmServiceInterface.Stub;
import com.service.LocalServiceTimer;
import com.service.LocalServiceTimer.AlarmServiceInterfaceImp;
import com.service.SilentService;

public class MyMapActivity extends MapActivity {
	private MyMapView myMapView;
	private MapController mapController;
	public final static Boolean DEVELOPER_MODE = false;
	private Boolean MapIntitialised = false;
	private MyLocationOverlay locationOverlay;
	private TabHost monTabHost;
	public static Location myLocation;
	public static GeoPoint myGeoPoint;
	public static Double mLatitude;
	public static Double mLongitude;
	public static Context mContext;
	private SharedPreferences app_preferences;
	public static boolean isNetworkConnected = false;

	public static Context getmContext() {
		return mContext;
	}

	public static void setmContext(Context mContext) {
		MyMapActivity.mContext = mContext;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		if (DEVELOPER_MODE) {
			// StrictMode.enableDefaults(); //2.2
		}

		HideTitleAndIconOfApplication(true);

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		IndependenteInitialise();
		InitialisationLocation();
		IntitialisationAnglet();
		IntialisationButtons();
		InitialisationServices();
//		if(isNetworkConnected)
//			InitialisationMarqueurs();

	}

	private void HideTitleAndIconOfApplication(boolean bool) {
		if (bool)
			requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	private void IndependenteInitialise() {
		mContext = this;
		app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
		
		Handler h = new Handler() { 
		    @Override
		    public void handleMessage(Message msg) {

		        if (msg.what != 1) { // code if not connected
		        	isNetworkConnected = false;
		        } else { // code if connected
		        	isNetworkConnected = true;
		        	 InitialisationMarqueurs();
		        }

		    }
		};
		Utils.isNetworkAvailable(h,2000);
		
	}

	private void InitialisationMarqueurs() {

		myMapView = Utils.MapMarqueurSetup(myMapView, this);

	}

	public TabHost getMonTabHost() {
		return monTabHost;
	}

	public void setMonTabHost(TabHost monTabHost) {
		this.monTabHost = monTabHost;
	}

	public void IntitialisationAnglet() {

		monTabHost = Utils.setupTabHost(
				(TabHost) findViewById(R.id.TabHostMenu), this);
	}

	private void InitialisationServices() {
		InitialisationSettingTabTimerNotification(this);
		InitialisationSettingTabSilentMode(this);
	}

	private void InitialisationLocation() {

		myMapView = (MyMapView) findViewById(R.id.mapView);
		mapController = myMapView.getController();
		mapController.setZoom(Param.ZOOM_LEVEL);
		myMapView.getZoomButtonsController();
		myMapView.setSatellite(true);

		locationOverlay = new MyLocationOverlay(MyMapActivity.this, myMapView);
		myMapView.getOverlays().add(locationOverlay);
		if (DEVELOPER_MODE) {
			mLatitude = 48.855279;// 36.859502;//
			mLongitude = 2.287939;// 10.168097;//
			myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
			mapController.animateTo(myGeoPoint);

		} else {
			InitialisationCurrentLocation();
		}

		MapIntitialised = true;

	}

	ImageButton zoomInBotton;
	ImageButton zoomOutBotton;
	Button ajouterMosqueeSettingButtom;
	public static ImageButton ajouterMosqueeTagButton;
	public static Button terminerajouterMosqueeTagButton;
	Button autoSilentSettingsButton;

	private void IntialisationButtons() {
		zoomInBotton = (ImageButton) findViewById(R.id.ZoomInButton);
		zoomOutBotton = (ImageButton) findViewById(R.id.ZoomOutButton);
		ajouterMosqueeSettingButtom = (Button) findViewById(R.id.boutton_ajout_setting_mosquee);
		ajouterMosqueeTagButton = (ImageButton) findViewById(R.id.ajout_mosque_tag_buttom);
		terminerajouterMosqueeTagButton = (Button) findViewById(R.id.terminer_ajout_mosque_tag_buttom);
		autoSilentSettingsButton = (Button) findViewById(R.id.boutton_congif_mise_en_mode_selencieux_heure_priere);

		zoomInBotton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mapController.zoomIn();
			}
		});
		zoomOutBotton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mapController.zoomOut();

			}
		});

		ajouterMosqueeSettingButtom.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				AjouterMosque.EditAjoutMosqueEnabling();
			}

		});
		ajouterMosqueeTagButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (!AjouterMosque.isTagButtonActivate) {
					ajouterMosqueeTagButton
							.setBackgroundResource(R.drawable.light_grey_square);
					AjouterMosque.isTagButtonActivate = true;
				} else {
					ajouterMosqueeTagButton
							.setBackgroundResource(R.drawable.dk_grey_square);
					AjouterMosque.isTagButtonActivate = false;
				}

			}

		});
		terminerajouterMosqueeTagButton
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						AjouterMosque.TeminerAjoutMosque(MyMapActivity
								.getmContext());

					}

				});

		autoSilentSettingsButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				final ScrollView scroller = (ScrollView) findViewById(R.id.OngletSetting);
				final LinearLayout layout = (LinearLayout) findViewById(R.id.auto_silent_settings_layout_mother);
				if (!isSilentSettingPrayTimeDisplayed) {
					isSilentSettingPrayTimeDisplayed = true;
					autoSilentSettingsButton
							.setCompoundDrawablesWithIntrinsicBounds(
									R.drawable.mute_blue, R.drawable.nop,
									R.drawable.btn_dropdown_up, R.drawable.nop);
					layout.setLayoutParams(new LinearLayout.LayoutParams(
							android.view.ViewGroup.LayoutParams.FILL_PARENT,
							android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
				} else {
					isSilentSettingPrayTimeDisplayed = false;
					autoSilentSettingsButton
							.setCompoundDrawablesWithIntrinsicBounds(
									R.drawable.mute_blue, R.drawable.nop,
									R.drawable.btn_dropdown_down,
									R.drawable.nop);
					layout.setLayoutParams(new LinearLayout.LayoutParams(
							android.view.ViewGroup.LayoutParams.FILL_PARENT, 0));
				}

				final Handler mHandler = new Handler();
				final Runnable mUpdateTimeTask = new Runnable() {
					public void run() {

						scroller.fullScroll(View.FOCUS_DOWN);
					}
				};
				mHandler.removeCallbacks(mUpdateTimeTask);
				mHandler.postDelayed(mUpdateTimeTask, 50);

			}
		});
	}

	private boolean isSilentSettingPrayTimeDisplayed = false;

	public int inc(int i) {
		return i++;
	}

	@Override
	public void onResume() {
		super.onResume();
		if (MapIntitialised)
			locationOverlay.enableCompass();
	}

	@Override
	public void onPause() {
		super.onPause();
		if (MapIntitialised)
			locationOverlay.disableCompass();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (LocalServiceTimer.isServiceStarted) {
			unbindService(maConnexionServiceNotification);
		}
		if (SilentService.isServiceStarted)
			unbindService(maConnexionServiceSilentMode);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	static AlarmServiceInterfaceImp _serviceInterface;
	public static ServiceConnection maConnexionServiceNotification = new ServiceConnection() {

		public void onServiceConnected(ComponentName name, IBinder service) {

			_serviceInterface = (AlarmServiceInterfaceImp) Stub
					.asInterface(service);

		}

		public void onServiceDisconnected(ComponentName name) {
			_serviceInterface = null;
		}
	};

	private SilentService monService;
	private ServiceConnection maConnexionServiceSilentMode = new ServiceConnection() {

		public void onServiceConnected(ComponentName className, IBinder service) {
			monService = ((SilentService.MonServiceSilentBinder) service)
					.getService();

		}

		public void onServiceDisconnected(ComponentName className) {
			monService = null;
		}
	};

	public static Intent IntentServiceNotification(Context context) {

		return new Intent(context, LocalServiceTimer.class);
	}

	public static Intent IntentServiceSilentMode(Context context) {

		return new Intent(context, SilentService.class);
	}

	public void InitialisationSettingTabTimerNotification(final Context context) {

		// spinner rayon

		String[] lischoises = getResources().getStringArray(
				R.array.rayon_nb_kilometre);
		ArrayAdapter<String> adapterRayon = new ArrayAdapter<String>(context,
				R.layout.my_spinner_style_arabe_adapter, lischoises);

		Spinner spinnerRayon = (Spinner) findViewById(R.id.RayonSetting);
		spinnerRayon.setAdapter(adapterRayon);
		spinnerRayon
				.setOnItemSelectedListener(new MyOnItemSelectedListenerRayonSetting());
		String rayonState = app_preferences.getString(Param.PREF_RAYON, "1");

		DataBaseQuery.rayon = Double.parseDouble(rayonState);
		if (rayonState.equals("5"))
			spinnerRayon.setSelection(0);
		else if (rayonState.equals("10"))
			spinnerRayon.setSelection(1);
		else if (rayonState.equals("100"))
			spinnerRayon.setSelection(2);
		else if (rayonState.equals("1000"))
			spinnerRayon.setSelection(3);
		// spinner Notification
		Spinner spinnerNotification = (Spinner) findViewById(R.id.NotificationSalatSetting);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.notification_setting_choises,
				android.R.layout.simple_spinner_dropdown_item);

		spinnerNotification.setAdapter(adapter);
		spinnerNotification
				.setOnItemSelectedListener(new MyOnItemSelectedListenerNotificationSetting());

		String notificationState = app_preferences.getString(
				Param.PREF_SERVICE_NOTIFICATION_STATE, Param.STATE_DISABLED);
		if (notificationState.equals(Param.STATE_ENABLED)) {
			spinnerNotification.setSelection(0);
			Utils.notificationChoises = Utils.NotificationChoises.DESACTIVER;
		} else if (notificationState.equals(Param.STATE_ENABLED)) {
			bindServiceNotification();
			LocalServiceTimer.isServiceStarted = true;
			spinnerNotification.setSelection(1);
			Utils.notificationChoises = Utils.NotificationChoises.ACTIVER;
		} else if (notificationState.equals(Param.STATE_ENABLED_SOUND)) {
			bindServiceNotification();
			LocalServiceTimer.isServiceStarted = true;
			spinnerNotification.setSelection(2);
			Utils.notificationChoises = Utils.NotificationChoises.ACTIVER_SON;
		} else if (notificationState.equals(Param.STATE_ENABLED_VIBRATION)) {
			bindServiceNotification();
			LocalServiceTimer.isServiceStarted = true;
			spinnerNotification.setSelection(3);
			Utils.notificationChoises = Utils.NotificationChoises.ACTIVER_VIBRATION;
		} else if (notificationState
				.equals(Param.STATE_ENABLED_SOUND_VIBRATION)) {
			bindServiceNotification();
			LocalServiceTimer.isServiceStarted = true;
			spinnerNotification.setSelection(4);
			Utils.notificationChoises = Utils.NotificationChoises.ACTIVER_SON_VIBRATION;
		}

		// spinnermethode de calcul
		Spinner spinnerMethCal = (Spinner) findViewById(R.id.MethCalculSalatSpinner);
		ArrayAdapter<CharSequence> adapterMethCal = ArrayAdapter
				.createFromResource(this, R.array.meth_cal_priere,
						android.R.layout.simple_spinner_dropdown_item);

		spinnerMethCal.setAdapter(adapterMethCal);
		spinnerMethCal
				.setOnItemSelectedListener(new MyOnItemSelectedListenerMethoCalcPriere());// !!!
		int methCalState = app_preferences.getInt(
				Param.PREF_PRAY_TIME_CAL_METHODE, 0);

		spinnerMethCal.setSelection(methCalState);

	}

	public void InitialisationSettingTabSilentMode(final Context context) {

		ToggleButton toggleButtonSilentMode = (ToggleButton) findViewById(R.id.auto_silent_activat_service);
		toggleButtonSilentMode
				.setOnClickListener(new MyOnClickListenerSilentMode());
		// config du service aut-silent-mode-pray-time
		Boolean silentSeviceState = app_preferences.getString(
				Param.PREF_SERVICE_SILENT_LAUNCHED, Param.STATE_DISABLED)
				.equals(Param.STATE_ENABLED);

		if (silentSeviceState) {
			SilentService.isServiceStarted = true;
			bindServiceSilentMode();

			((ToggleButton) findViewById(R.id.auto_silent_activat_service))
					.setChecked(true);
		} else
			((ToggleButton) findViewById(R.id.auto_silent_activat_service))
					.setChecked(false);
		// _______________________
		String text = "";
		EditText edit_auto_silent_fajr_arg1 = (EditText) findViewById(R.id.auto_silent_fajr_arg1);
		edit_auto_silent_fajr_arg1
				.addTextChangedListener(new MySilentModeTextWatcher(
						edit_auto_silent_fajr_arg1));
		text = app_preferences.getInt(Param.PREF_FAJR_PERIODE, -1) + "";
		edit_auto_silent_fajr_arg1.setText(
				app_preferences.getInt(Param.PREF_FAJR_PERIODE, -1) + "",
				TextView.BufferType.EDITABLE);
		if (text.equals("-1"))
			edit_auto_silent_fajr_arg1
					.setText("", TextView.BufferType.EDITABLE);
		// _______________________
		text = app_preferences.getInt(Param.PREF_FAJR_DUREE, -1) + "";
		EditText edit_auto_silent_fajr_arg2 = (EditText) findViewById(R.id.auto_silent_fajr_arg2);
		edit_auto_silent_fajr_arg2
				.addTextChangedListener(new MySilentModeTextWatcher(
						edit_auto_silent_fajr_arg2));
		edit_auto_silent_fajr_arg2.setText(text, TextView.BufferType.EDITABLE);
		if (text.equals("-1"))
			edit_auto_silent_fajr_arg2
					.setText("", TextView.BufferType.EDITABLE);
		// _______________________
		text = app_preferences.getInt(Param.PREF_DHOHR_DUREE, -1) + "";
		EditText edit_auto_silent_dhohr_arg1 = (EditText) findViewById(R.id.auto_silent_dhohr_arg1);
		edit_auto_silent_dhohr_arg1
				.addTextChangedListener(new MySilentModeTextWatcher(
						edit_auto_silent_dhohr_arg1));
		edit_auto_silent_dhohr_arg1.setText(text, TextView.BufferType.EDITABLE);
		if (text.equals("-1"))
			edit_auto_silent_dhohr_arg1.setText("",
					TextView.BufferType.EDITABLE);
		// _______________________
		text = app_preferences.getInt(Param.PREF_DHOHR_DUREE, -1) + "";
		EditText edit_auto_silent_dhohr_arg2 = (EditText) findViewById(R.id.auto_silent_dhohr_arg2);
		edit_auto_silent_dhohr_arg2
				.addTextChangedListener(new MySilentModeTextWatcher(
						edit_auto_silent_dhohr_arg2));
		edit_auto_silent_dhohr_arg2.setText(text, TextView.BufferType.EDITABLE);
		if (text.equals("-1"))
			edit_auto_silent_dhohr_arg2.setText("",
					TextView.BufferType.EDITABLE);
		// _______________________
		text = app_preferences.getInt(Param.PREF_ASR_PERIODE, -1) + "";
		EditText edit_auto_silent_asr_arg1 = (EditText) findViewById(R.id.auto_silent_asr_arg1);
		edit_auto_silent_asr_arg1
				.addTextChangedListener(new MySilentModeTextWatcher(
						edit_auto_silent_asr_arg1));
		edit_auto_silent_asr_arg1.setText(text, TextView.BufferType.EDITABLE);
		if (text.equals("-1"))
			edit_auto_silent_asr_arg1.setText("", TextView.BufferType.EDITABLE);
		// _______________________
		text = app_preferences.getInt(Param.PREF_ASR_DUREE, -1) + "";
		EditText edit_auto_silent_asr_arg2 = (EditText) findViewById(R.id.auto_silent_asr_arg2);
		edit_auto_silent_asr_arg2
				.addTextChangedListener(new MySilentModeTextWatcher(
						edit_auto_silent_asr_arg2));
		edit_auto_silent_asr_arg2.setText(text, TextView.BufferType.EDITABLE);
		if (text.equals("-1"))
			edit_auto_silent_asr_arg2.setText("", TextView.BufferType.EDITABLE);
		// _______________________
		text = app_preferences.getInt(Param.PREF_MAGHREB_PERIODE, -1) + "";
		EditText edit_auto_silent_maghreb_arg1 = (EditText) findViewById(R.id.auto_silent_maghreb_arg1);
		edit_auto_silent_maghreb_arg1
				.addTextChangedListener(new MySilentModeTextWatcher(
						edit_auto_silent_maghreb_arg1));
		edit_auto_silent_maghreb_arg1.setText(text,
				TextView.BufferType.EDITABLE);
		if (text.equals("-1"))
			edit_auto_silent_maghreb_arg1.setText("",
					TextView.BufferType.EDITABLE);
		// _______________________
		text = app_preferences.getInt(Param.PREF_MAGHREB_DUREE, -1) + "";
		EditText edit_auto_silent_maghreb_arg2 = (EditText) findViewById(R.id.auto_silent_maghreb_arg2);
		edit_auto_silent_maghreb_arg2
				.addTextChangedListener(new MySilentModeTextWatcher(
						edit_auto_silent_maghreb_arg2));
		edit_auto_silent_maghreb_arg2.setText(text,
				TextView.BufferType.EDITABLE);
		if (text.equals("-1"))
			edit_auto_silent_maghreb_arg2.setText("",
					TextView.BufferType.EDITABLE);
		// _______________________
		text = app_preferences.getInt(Param.PREF_ISHA_PERIODE, -1) + "";
		EditText edit_auto_silent_isha_arg1 = (EditText) findViewById(R.id.auto_silent_isha_arg1);
		edit_auto_silent_isha_arg1
				.addTextChangedListener(new MySilentModeTextWatcher(
						edit_auto_silent_isha_arg1));
		edit_auto_silent_isha_arg1.setText(text, TextView.BufferType.EDITABLE);
		if (text.equals("-1"))
			edit_auto_silent_isha_arg1
					.setText("", TextView.BufferType.EDITABLE);
		// _______________________
		text = app_preferences.getInt(Param.PREF_ISHA_DUREE, -1) + "";
		EditText edit_auto_silent_isha_arg2 = (EditText) findViewById(R.id.auto_silent_isha_arg2);
		edit_auto_silent_isha_arg2
				.addTextChangedListener(new MySilentModeTextWatcher(
						edit_auto_silent_isha_arg2));
		edit_auto_silent_isha_arg2.setText(text, TextView.BufferType.EDITABLE);
		if (text.equals("-1"))
			edit_auto_silent_isha_arg2
					.setText("", TextView.BufferType.EDITABLE);

	}

	private void startServiceNotification() {

		startService(IntentServiceNotification(this));

	}

	private void stopServiceNotification() {

		stopService(IntentServiceNotification(this));
	} 

	private void bindServiceNotification() {

		bindService(IntentServiceNotification(this),
				maConnexionServiceNotification, Context.BIND_AUTO_CREATE);

	}

	private void releaseServiceNotification() {

		unbindService(maConnexionServiceNotification);

	}

	private void startServiceSilentMode() {

		startService(IntentServiceSilentMode(MyMapActivity.this));

	}

	private void stopServiceSilentMode() {

		stopService(IntentServiceSilentMode(this));

	}

	private void bindServiceSilentMode() {

		bindService(IntentServiceSilentMode(this),
				maConnexionServiceSilentMode, Context.BIND_AUTO_CREATE);

	}

	private void releaseServiceSilentMode() {

		unbindService(maConnexionServiceSilentMode);

	}

	private class MyOnClickListenerSilentMode implements OnClickListener {

		public void onClick(View v) {
			final SharedPreferences.Editor editor = app_preferences.edit();
			if (((ToggleButton) v).isChecked()) {

				EditText edit_auto_silent_fajr_arg1 = (EditText) findViewById(R.id.auto_silent_fajr_arg1);
				Boolean fajr_arg1 = edit_auto_silent_fajr_arg1.getText()
						.length() > 0;
				EditText edit_auto_silent_fajr_arg2 = (EditText) findViewById(R.id.auto_silent_fajr_arg2);
				Boolean fajr_arg2 = edit_auto_silent_fajr_arg2.getText()
						.length() > 0;

				EditText edit_auto_silent_dhohr_arg1 = (EditText) findViewById(R.id.auto_silent_dhohr_arg1);
				Boolean dhohr_arg1 = edit_auto_silent_dhohr_arg1.getText()
						.length() > 0;
				EditText edit_auto_silent_dhohr_arg2 = (EditText) findViewById(R.id.auto_silent_dhohr_arg2);
				Boolean dhohr_arg2 = edit_auto_silent_dhohr_arg2.getText()
						.length() > 0;

				EditText edit_auto_silent_asr_arg1 = (EditText) findViewById(R.id.auto_silent_asr_arg1);
				Boolean asr_arg1 = edit_auto_silent_asr_arg1.getText().length() > 0;
				EditText edit_auto_silent_asr_arg2 = (EditText) findViewById(R.id.auto_silent_asr_arg2);
				Boolean asr_arg2 = edit_auto_silent_asr_arg2.getText().length() > 0;

				EditText edit_auto_silent_maghreb_arg1 = (EditText) findViewById(R.id.auto_silent_maghreb_arg1);
				Boolean maghreb_arg1 = edit_auto_silent_maghreb_arg1.getText()
						.length() > 0;
				EditText edit_auto_silent_maghreb_arg2 = (EditText) findViewById(R.id.auto_silent_maghreb_arg2);
				Boolean maghreb_arg2 = edit_auto_silent_maghreb_arg2.getText()
						.length() > 0;

				EditText edit_auto_silent_isha_arg1 = (EditText) findViewById(R.id.auto_silent_isha_arg1);
				Boolean isha_arg1 = edit_auto_silent_isha_arg1.getText()
						.length() > 0;
				EditText edit_auto_silent_isha_arg2 = (EditText) findViewById(R.id.auto_silent_isha_arg2);
				Boolean isha_arg2 = edit_auto_silent_isha_arg2.getText()
						.length() > 0;

				if (fajr_arg1 && fajr_arg2 && dhohr_arg1 && dhohr_arg2
						&& maghreb_arg1 && asr_arg2 && asr_arg1 && maghreb_arg2
						&& isha_arg1 && isha_arg2) {

					if (SilentService.isServiceStarted) {
						releaseServiceSilentMode();
						stopServiceSilentMode();
					}
					bindServiceSilentMode();
					startServiceSilentMode();
					SilentService.isServiceStarted = true;
					editor.putString(Param.PREF_SERVICE_SILENT_LAUNCHED,
							Param.STATE_ENABLED);
				} else {

					String text = mContext
							.getString(R.string.put_all_empty_box_request);
					Toast.makeText(MyMapActivity.getmContext(), text,
							Toast.LENGTH_LONG).show();
					((ToggleButton) v).setChecked(false);

				}

			}

			else if (SilentService.isServiceStarted) {
				releaseServiceSilentMode();
				stopServiceSilentMode();
				SilentService.isServiceStarted = false;
				editor.putString(Param.PREF_SERVICE_SILENT_LAUNCHED,
						Param.STATE_DISABLED);
			}
			editor.commit();
		}

	}

	private class MySilentModeTextWatcher implements TextWatcher {
		final SharedPreferences.Editor editor = app_preferences.edit();

		private EditText mEditText;

		public MySilentModeTextWatcher(EditText e) {
			mEditText = e;
		}

		public void afterTextChanged(Editable s) {
			String salat = "";
			switch (mEditText.getId()) {
			case R.id.auto_silent_fajr_arg1:
				salat = Param.PREF_FAJR_PERIODE;
				break;
			case R.id.auto_silent_fajr_arg2:
				salat = Param.PREF_FAJR_DUREE;
				break;
			case R.id.auto_silent_dhohr_arg1:
				salat = Param.PREF_DHOHR_PERIODE;
				break;
			case R.id.auto_silent_dhohr_arg2:
				salat = Param.PREF_DHOHR_DUREE;
				break;
			case R.id.auto_silent_asr_arg1:
				salat = Param.PREF_ASR_PERIODE;
				break;
			case R.id.auto_silent_asr_arg2:
				salat = Param.PREF_ASR_DUREE;
				break;
			case R.id.auto_silent_maghreb_arg1:
				salat = Param.PREF_MAGHREB_PERIODE;
				break;
			case R.id.auto_silent_maghreb_arg2:
				salat = Param.PREF_MAGHREB_DUREE;
				break;
			case R.id.auto_silent_isha_arg1:
				salat = Param.PREF_ISHA_PERIODE;
				break;
			case R.id.auto_silent_isha_arg2:
				salat = Param.PREF_ISHA_DUREE;
				break;
			}
			if (s.toString().length() > 0) {
				editor.putInt(salat, Integer.parseInt(s.toString()));
			} else
				editor.putInt(salat, -1);
			editor.commit();

		}

		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub

		}

		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			// TODO Auto-generated method stub

		}

	}

	private class MyOnItemSelectedListenerNotificationSetting implements
			OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			SharedPreferences.Editor editor = app_preferences.edit();

			switch (pos) {
			case 0:
				Utils.notificationChoises = Utils.NotificationChoises.DESACTIVER;
				editor.putString(Param.PREF_SERVICE_NOTIFICATION_STATE,
						Param.STATE_DISABLED);
				editor.commit();
				break;
			case 1:
				Utils.notificationChoises = Utils.NotificationChoises.ACTIVER;
				editor.putString(Param.PREF_SERVICE_NOTIFICATION_STATE,
						Param.STATE_ENABLED);
				editor.commit();
				break;
			case 2:
				Utils.notificationChoises = Utils.NotificationChoises.ACTIVER_SON;
				editor.putString(Param.PREF_SERVICE_NOTIFICATION_STATE,
						Param.STATE_ENABLED_SOUND);
				editor.commit();
				break;
			case 3:
				Utils.notificationChoises = Utils.NotificationChoises.ACTIVER_VIBRATION;
				editor.putString(Param.PREF_SERVICE_NOTIFICATION_STATE,
						Param.STATE_ENABLED_SOUND_VIBRATION);
				editor.commit();
				break;
			case 4:
				Utils.notificationChoises = Utils.NotificationChoises.ACTIVER_SON_VIBRATION;
				editor.putString(Param.PREF_SERVICE_NOTIFICATION_STATE,
						Param.STATE_ENABLED_SOUND_VIBRATION);
				editor.commit();
				break;
			}

			if (Utils.notificationChoises != Utils.NotificationChoises.DESACTIVER) {

				if (LocalServiceTimer.isServiceStarted) {
					releaseServiceNotification();
					stopServiceNotification();
				}

				bindServiceNotification();
				startServiceNotification();
				LocalServiceTimer.isServiceStarted = true;

			} else if (LocalServiceTimer.isServiceStarted) {
				releaseServiceNotification();
				stopServiceNotification();
				LocalServiceTimer.isServiceStarted = false;

			}
		}

		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}

	}

	public class MyOnItemSelectedListenerRayonSetting implements
			OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			SharedPreferences.Editor editor = app_preferences.edit();

			switch (pos) {
			case 0:

				editor.putString(Param.RAYON, "5");
				editor.commit();
				break;
			case 1:

				editor.putString(Param.RAYON, "10");
				editor.commit();
				break;
			case 2:

				editor.putString(Param.RAYON, "100");
				editor.commit();
				break;
			case 3:

				editor.putString(Param.RAYON, "1000");
				editor.commit();
				break;

			}

			DataBaseQuery.rayonChanged = true;
		}

		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}

	}

	public class MyOnItemSelectedListenerMethoCalcPriere implements
			OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			SharedPreferences.Editor editor = app_preferences.edit();

			editor.putInt(Param.PREF_PRAY_TIME_CAL_METHODE, pos);
			editor.commit();
			PrayTimeLayout.Renitialisation(MyMapActivity.mContext, pos);
		}

		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}

	}

	Location lastKnownLocation;

	private void InitialisationCurrentLocation() {

		LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		LocationListener mlocListener = new MyLocationListener();

		mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
				0, mlocListener);
		for (String provider : mlocManager.getAllProviders()) {
			Location loc = mlocManager.getLastKnownLocation(provider);
			if (loc != null) {
				myLocation = loc;
			}
		}
		
		
		if (myLocation != null) {
			mLatitude = myLocation.getLatitude();
			mLongitude = myLocation.getLongitude();
			myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
			mapController.animateTo(myGeoPoint);
			SharedPreferences.Editor editor = app_preferences.edit();
			editor.putString(Param.PREF_LATITUDE, mLatitude + "");
			editor.putString(Param.PREF_LONGITUDE, mLongitude + "");
			editor.commit();
		} else {

			String latitude_buff = app_preferences.getString(
					Param.PREF_LATITUDE, "");
			String longitude_buff = app_preferences.getString(
					Param.PREF_LONGITUDE, "");
			if (latitude_buff.length() > 0 && longitude_buff.length() > 0) {

				mLatitude = Double.parseDouble(latitude_buff);
				mLongitude = Double.parseDouble(longitude_buff);
				myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
				mapController.animateTo(myGeoPoint);
			} else {

				AlertDialog.Builder builder = new AlertDialog.Builder(
						MyMapActivity.getmContext());
				builder.setMessage(
						MyMapActivity.getmContext().getString(
								R.string.dialog_need_to_connect_advice))
						.setCancelable(true)
						.setNegativeButton(
								MyMapActivity.getmContext().getString(
										R.string.dialog_ok),
								new DialogInterface.OnClickListener() {
									public void onClick(
											DialogInterface dialog, int id) {
										dialog.cancel();
									}
								});
				AlertDialog alert = builder.create();
				alert.show();

				mLatitude = 36.859502;// 48.855279;//
				mLongitude = 10.168097;// 2.287939;//
				myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
				mapController.animateTo(myGeoPoint);
			}
		}

	}


	public class MyLocationListener implements LocationListener

	{

		public void onLocationChanged(Location loc)

		{
			if (loc != null) {
				mLatitude = loc.getLatitude();
				mLongitude = loc.getLongitude();
				myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
				mapController.animateTo(myGeoPoint);
				SharedPreferences.Editor editor = app_preferences.edit();
				editor.putString(Param.PREF_LATITUDE, mLatitude + "");
				editor.putString(Param.PREF_LONGITUDE, mLongitude + "");
				editor.commit();
			}
			
			

		}

		public void onProviderDisabled(String provider)

		{

//			Toast.makeText(getApplicationContext(), "Disabled",
//
//			Toast.LENGTH_SHORT).show();

		}

		public void onProviderEnabled(String provider)

		{

		}

		public void onStatusChanged(String provider, int status, Bundle extras)

		{

		}

	}

}
// Saisir les
// Toast.makeText(MyMapActivity.getmContext() , "Time :" ,
// Toast.LENGTH_LONG).show();

// retourner une ressource
// context.getString(R.string.loading)

/*
 * Set on full screan (au debut de onCreate)
 * this.requestWindowFeature(Window.FEATURE_NO_TITLE);
 * this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
 * WindowManager.LayoutParams.FLAG_FULLSCREEN );
 * super.onCreate(savedInstanceState);
 */